﻿<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>培训信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_PersonTrainBatcheId_FormItem" prop="Entity.PersonTrainBatcheId">
								<template #label>
									<WtmLabelTooltip label="培训批次" tooltip="关联的培训项目" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.PersonTrainBatcheId" clearable filterable class="width100">
									<el-option v-for="item in stateData.AllSysPersonTrainBatches" :key="item.Value" :value="item.Value" :label="item.Text"></el-option
								></el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>考试信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Score_FormItem" prop="Entity.Score">
								<template #label>
									<WtmLabelTooltip label="成绩" tooltip="考试成绩" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.Score" clearable class="width100"></el-input-number>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>培训信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_ScoreReportId_FormItem" prop="Entity.ScoreReportId">
								<template #label>
									<WtmLabelTooltip label="成绩单" tooltip="成绩单文件" />
								</template>
								<wtm-upload-file v-model="stateData.vmModel.Entity.ScoreReportId" clearable class="width100"></wtm-upload-file>
							</el-form-item>
						</el-col>
					</el-row>
				</div>
			</el-form>
		</el-card>
	</div>
</template>

<script setup lang="ts" name="message._system.common.vm.add;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted, nextTick } from 'vue';
import { SysPersonTrainExamApi as api } from '/@/views/person/syspersontrainexam/api';
import other from '/@/utils/other';
import { ISysPersonTrainExamVm } from '/@/views/person/syspersontrainexam/api/interface';
import { IViewModel } from '/@/types/common';

const ci = getCurrentInstance() as any;
const loading = ref(false);

// 定义变量内容
const formRef = ref();
const stateData = reactive<IViewModel<ISysPersonTrainExamVm>>({
	vmModel: {
		Entity: {
			PersonId: null,
			PersonTrainBatcheId: null,
			Score: null,
			ScoreReportId: null,
		},
	},

	AllSysPersons: [] as any[],
	AllSysPersonTrainBatches: [] as any[],
});

// 取消
const onClose = () => {
	closeDialog();
};

// 提交
const onSubmit = () => {
	formRef.value?.validate((valid: boolean) => {
		if (valid) {
			loading.value = true;
			api()
				.add(stateData.vmModel)
				.then(() => {
					ElMessage.success(ci.proxy.$t('message._system.common.vm.submittip'));
					emit('refresh');
					closeDialog();
				})
				.catch((error) => {
					other.setFormError(ci, error);
				})
				.finally(() => {
					loading.value = false;
				});
		}
	});
};

// 页面加载时
onMounted(() => {
	loading.value = true;
	if (ci.attrs['wtmdata']) {
		stateData.vmModel.Entity.ID = ci.attrs['wtmdata'].ID;
		// 如果传入了PersonId，设置到实体中
		if (ci.attrs['wtmdata'].PersonId) {
			stateData.vmModel.Entity.PersonId = ci.attrs['wtmdata'].PersonId;
		}
	}
	const promises: Promise<any>[] = [

		other.getSelectList('/api/SysPersonTrainExam/GetSysPersonTrainBatches', [], false).then((x) => {
			stateData.AllSysPersonTrainBatches = x;
		}),
	];
	Promise.allSettled(promises).finally(() => {
		loading.value = false;
		nextTick(() => {
			setupHeaderButtons();
		});
	});
});

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh', 'closeDialog','updateHeaderButtons']);
// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};

// 设置对话框头部按钮
const setupHeaderButtons = () => {
	const headerButtons = [
		{
			text: ci.proxy.$t('message._system.common.vm.submit'),
			type: 'primary',
			onClick: onSubmit,
			visible: true,
		},
		{
			text: ci.proxy.$t('message._system.common.vm.cancel'),
			type: 'default',
			onClick: onClose,
			visible: true,
		},
	];

	emit('updateHeaderButtons', headerButtons);
};
</script>
